/**
 * @author Hemant
 * Displaytext.java
 * This class handles the displaytext objects. It fetches data from displaytext object's "text"
 * properties or from reports and merge it to response string.
 */


/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guiproperty;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Hashtable;
import java.util.LinkedList;

import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.CapitalChar;
import utility.Input;
import utility.Replace;

public class Displaytext implements HTMLInterface 
{
	private String header,ObjectName,tag="",report,line,param = "",text="";
	private LinkedList<Object> PropList;
	Hashtable<Object, Object> hash;
	private Hashtable<Object, LinkedList<Object>> ObjProp;
	StringBuffer buffer;
	GetIdName get;
	String reportpath = null;
	
	/**
	 * This method sets object name for the object name which is present 
	 * in hashtable with its properties. 
	 * @param Objectname Object name of Displaytext object.
	 */
	public void setObjectName(String ObjectName) 
	{
		this.ObjectName = ObjectName;		
	}
	
	/**
	 * This method accepts Hashtable containing tag objects and its properties.
	 * It extracts the object specific property list.
	 * @param ObjectPropertyList Hashtable containing objects and its properties.
	 */
	public void setObjPropertyList(Hashtable<Object, LinkedList<Object>> ObjectPropertyList) 
	{
		this.ObjProp = ObjectPropertyList;
		this.PropList = this.ObjProp.get(this.ObjectName);
	}
	
	/**
	 * It generates string data for Displaytext object using its proertylist
	 * @return String of test data
	 */
	public String getHTMLCode()
	{
		try {
			int size = PropList.size();
			buffer = new StringBuffer();
			buffer.append("");		
			
			//This for loop iterates over propertylist of the attribute object
			for(int i = 0;i < size;i++ )
			{
				//Extract ith element from propertylist and split it on special char to get an array.
				//The array will contain attribute and its value.
				String [] property = PropList.get(i).toString().split(Input.SEPARATOR);
				System.out.println("The property array is "+PropList.get(i));
				
				// if the property is test then simply append the property value in response string
				if(property[0].equalsIgnoreCase("text"))
				{
					buffer.append(property[1]);
				}
				//if the property is report the it creates object of report class
				// pass it the property value as parameter.The getData() method of report 
				//class returns the path of text file in which test data is stored.
				// Using this report path readFile() method retirns the text data.
				else if(property[0].equalsIgnoreCase("report"))
				{
					report = property[1];		
					System.out.println("The report name is "+report);
			        guireportgenerator.Report rep = new guireportgenerator.Report(report);
			        rep.setGetIdName(get);
			        reportpath = rep.getData(); 
			        buffer.append(reportpath);//buffer.append(readFile());
				}
				//if the data is stored at any remote location then readFile() method accepts that
				//url and returns text data. 
				else if(property[0].equalsIgnoreCase("url"))
				{
					reportpath = property[1];
					String tempstr = readFile();
					String temp [] = tempstr.toLowerCase().split("body");
					buffer.append(temp[1].subSequence(temp[1].indexOf(">")+1, temp[1].lastIndexOf("<")));	
				}	
			}
			
			//buffer = new StringBuffer(CapitalChar.makeFirstCharCapital(buffer.toString()));
			buffer = new StringBuffer(buffer.toString());
			System.out.println("The buffer in Displaytext is  "+buffer.toString());
			return buffer.toString();
		} catch (Exception e) {
			return "";
		}
	}
	
	/**
	 * This method reads a specific file indicated by reportpath at any locale or remote location 
	 * @return Data in specified file.
	 */
	private String readFile()
	{
		StringBuffer sbr = new StringBuffer();
		RandomAccessFile raf = null;
		try 
		{
			raf = new RandomAccessFile(reportpath,"r");
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
		
		try 
		{
			while((line = raf.readLine()) != null)
			{
				sbr.append(line+"\n");
			}
		}
		catch (NumberFormatException e) 
		{
			e.printStackTrace();		
			return null;
		}
		catch (IOException e) 
		{
			e.printStackTrace();
			return null;
		}
		
		return sbr.toString();
	}
	
	/**
	 * Sets text
	 * @param text Dispalytext text.
	 */
	public void setText(String text) 
	{
		this.text = text;
	}
	
	/**
	 * Sets GetIdName object
	 * @param get GetIdName object
	 * 
	 */
	public void setGetIdName(GetIdName get) 
	{
		this.get = get;		
	}
	
	/**
	 * Sets line od data
	 * @param line Data line.
	 */
	public void setLine(String line) 
	{
		this.line = line;
	}
	
	public void setParam(String param) 
	{
		// TODO Auto-generated method stub		
	}

	public void setReport(String report) 
	{
		// TODO Auto-generated method stub		
	}

	/**
	 * Sets HTML tag for this object
	 * @param tag HTML tag.
	 */
	public void setTag(String tag) 
	{
		this.tag = tag;
	}

	public void setParamHash(Hashtable<Object, Object> hash) {
		// TODO Auto-generated method stub
		
	}

	public void setNoSelectObject(String noselect) {
		// TODO Auto-generated method stub
		
	}

	public void setSelectObject(String select) {
		// TODO Auto-generated method stub
		
	}

	public void isEnd(boolean end) {
		// TODO Auto-generated method stub
		
	}

	public void setHeaderLine(String header) {
		// TODO Auto-generated method stub
		this.header = header;
	}

	
	
}
